package cntedu.sp04.server;

import cn.tedu.sa01.pojo.Item;
import cn.tedu.sa01.pojo.Order;
import cn.tedu.sa01.pojo.User;
import cn.tedu.sa01.server.OrderServer;
import cn.tedu.web.util.JsonResult;
import cntedu.sp04.feign.ItemClient;
import cntedu.sp04.feign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServerImpl implements OrderServer {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;
    @Override
    public Order getOrder(String id) {
        log.info("获取订单，id=",id);
        //远程调用商品，获取商品列表
        JsonResult<List<Item>>item = itemClient.getItem(id);
        //远程调用用户，获取用户
        JsonResult<User>user = userClient.getUserId(8);
        Order order = new Order( );
        order.setId(id);
        order.setUser(user.getData());
        order.setItems(item.getData());
        return order;
    }

    @Override
    public void addOrder(Order order) {
       log.info("添加订单，order=",order);
       itemClient.decreaseNumber(order.getItems());
       userClient.addScore(order.getUser().getId(),1000);
    }
}
